package com.xxx.mch.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.mch.domain.dto.DeterminationScoreDetailDto;
import com.xxx.mch.domain.dto.DeterminationScoreTemplateDto;
import com.xxx.mch.domain.po.DeterminationScore;
import com.xxx.mch.domain.po.DeterminationScoreTemplate;
import com.xxx.mch.domain.request.DeterminationQueryScoreRequest;
import com.xxx.mch.domain.request.DeterminationScoreTemplateRequest;
import com.xxx.mch.domain.request.RuleSearchRequest;
import com.xxx.mch.service.impl.DeterminationScoreServiceImpl;
import com.xxx.mch.utils.MyPageInfo;
import com.xxx.mch.utils.WrapMapper;
import com.xxx.mch.utils.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.math.BigDecimal;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定性分
 *
 * @author FengHaixin
 * @description 一句话描述该文件的用途
 * @date 2020/2/26
 */
@RestController
@RequestMapping ("determinationScores")
@Api (tags = "定性评价接口")
public class DeterminationScoreController
{
    @Autowired
    private DeterminationScoreServiceImpl determinationScoreService;


    @ApiOperation (value = "保存定性评价得分")
    @PostMapping (consumes = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper saveScore(@RequestBody List<DeterminationScore> qualityScore, @RequestParam String month)
    {
        determinationScoreService.saveScore(qualityScore, month);
        return WrapMapper.ok();
    }


    @GetMapping (produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<MyPageInfo<DeterminationScore>> getScores(@Validated DeterminationQueryScoreRequest queryScoreRequest)
    {
        PageInfo<DeterminationScore> pageInfo = determinationScoreService.queryScoreRecordList(queryScoreRequest);
        BigDecimal sumScore = determinationScoreService.querySumScore(queryScoreRequest);
        MyPageInfo<DeterminationScore> myPageInfo = new MyPageInfo<>();
        BeanUtils.copyProperties(pageInfo, myPageInfo);
        myPageInfo.setSumScore(sumScore);
        return WrapMapper.ok(myPageInfo);
    }


    @ApiOperation (value = "获取多人打分后的详情信息")
    @GetMapping (value = "scoreDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<DeterminationScoreDetailDto> getScoresMap(@Validated DeterminationQueryScoreRequest queryScoreRequest)
    {
        return WrapMapper.ok(determinationScoreService.queryScoreRecord(queryScoreRequest));
    }


    @GetMapping (value = "/{evaluateId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<DeterminationScoreTemplate>> getRules(@PathVariable Integer evaluateId)
    {
        List<DeterminationScoreTemplate> list = determinationScoreService.getTemplateRules(evaluateId);
        return WrapMapper.ok(list);
    }


    @GetMapping (value = "template", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<DeterminationScoreTemplateDto>> taskTemplate(@RequestParam (value = "parentId", required = false) Integer parentId,
                                                                     @RequestParam (value = "depth", defaultValue = "3") Integer depth)
    {
        return WrapMapper.ok(determinationScoreService.getTaskTemplate(parentId, depth));
    }


    @ApiOperation (value = "定性分细则搜索")
    @GetMapping (value = "rule/search", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<DeterminationScoreTemplateDto>> ruleSearch(@ModelAttribute @Validated RuleSearchRequest ruleSearchRequest)
    {
        return WrapMapper.ok(determinationScoreService.ruleSearch(ruleSearchRequest));
    }


    @PostMapping (value = "template", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper createOrUpdate(@RequestBody List<DeterminationScoreTemplateRequest> templateRequestList)
    {
        determinationScoreService.createOrUpdateTemplate(templateRequestList);
        return WrapMapper.ok();
    }


    @DeleteMapping (value = "template/{templateId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper deleteTemplate(@PathVariable ("templateId") Integer templateId)
    {
        determinationScoreService.deleteTemplate(templateId);
        return WrapMapper.ok();
    }

    @ApiOperation (value = "修改定性分锁定状态。1:加锁，不能再进行定性分打分")
    @PutMapping (value = "lock/{status}", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper updateDeterminationLock(@RequestParam String month, @RequestBody List<String> uidList, @PathVariable Integer status)
    {
        determinationScoreService.updateDeterminationLock(uidList, month, status);
        return WrapMapper.ok();
    }
}
